package com.swj.auth.service.impl;

import com.swj.common.core.constant.Constants;
import com.swj.common.core.model.SysLoginLog;
import com.swj.common.core.remoteService.RemoteLogService;
import com.swj.auth.service.ISysRecordLogService;
import com.swj.common.core.constant.SecurityConstants;
import com.swj.common.core.utils.ServletUtil;
import com.swj.common.core.utils.SysStringUtil;
import com.swj.common.core.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
/**
 * 记录日志方法
 *
 * @author ruoyi
 */
@Service
public class SysRecordLogServiceImpl implements ISysRecordLogService {
    @Autowired
    private RemoteLogService remoteLogService;

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息内容
     */
    @Override
    public void recordLogininfor(String username, String status, String message) {
        SysLoginLog logging = new SysLoginLog();
        logging.setUserName(username);
        logging.setIpaddr(IpUtils.getIpAddr(ServletUtil.getRequest()));
        logging.setMsg(message);
        // 处理日志status状态
        if (SysStringUtil.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            logging.setStatus(Constants.LOGIN_SUCCESS_STATUS);
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            logging.setStatus(Constants.LOGIN_FAIL_STATUS);
        }
        logging.setCreateTime(LocalDateTime.now());
        logging.setCreateBy(username);
        remoteLogService.saveLogininfor(logging, SecurityConstants.INNER);
    }
}
